package com.test.personalblogproject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.personalblogproject.pojo.Article;
import com.test.personalblogproject.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Insert("INSERT INTO ms_article (title, summary, author_id, category_id, create_date, comment_counts, view_counts, weight) " +
            "VALUES (#{title}, #{summary}, #{authorId}, #{categoryId}, #{createDate}, #{commentCounts}, #{viewCounts}, #{weight})")
    @Options(useGeneratedKeys = true, keyProperty = "id")   // 主键回填
    int insert(Article article);

    @Update("UPDATE ms_article SET body_id = #{bodyId} WHERE id = #{id}")
    void updatebodyId(@Param("id") Long id, @Param("bodyId") Long bodyId);
    @Delete(" DELETE FROM ms_article where id = #{id}")
    public boolean deleteById(Long id);



    /** 点赞：返回 1 表示成功，0 表示已点过 */
    @Insert("INSERT IGNORE INTO user_like(user_id,article_id) VALUES(#{userId},#{articleId})")
    int insert1(@Param("userId") int userId, @Param("articleId") Long articleId);

    /** 取消点赞 */
    @Delete("DELETE FROM user_like WHERE user_id=#{userId} AND article_id=#{articleId}")
    int delete(@Param("userId") int userId, @Param("articleId") Long articleId);

    /** 是否已点赞 */
    @Select("SELECT COUNT(1) FROM user_like WHERE user_id=#{userId} AND article_id=#{articleId}")
    int count(@Param("userId") int userId, @Param("articleId") Long articleId);

    // 用户喜欢的全部文章id
    @Select("SELECT article_id FROM user_like WHERE user_id=#{userId} ORDER BY liked_time DESC")
    List<Integer> selectArticleIdsByUser(@Param("userId") int userId);

    @Select("SELECT COUNT(1) FROM user_like WHERE article_id = #{articleId}")
    int countLikes(@Param("articleId") int articleId);

}